package com.yanqu.road.dao.impl.taobao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import taobao.TaoBaoLabel;
import taobao.UserTaoBaoData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserTaoBaoDataDaoImpl extends TempDataObjectDao<UserTaoBaoData> {
    public static String getUserTaoBaoDataSql = "select * from `t_u_taobao_data` where `activityId`=? and `userId`=?";
    public UserTaoBaoData getUserTaoBaoData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserTaoBaoDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserTaoBaoDataSql = "update `t_u_taobao_data` set itemId=?,initPrice=?,labelList=?,selectedLabelList=?,priceFluctuationList=?,resetTime=?,dealStatus=?,playStatus=?,priceStatus=?,earnPrice=? where activityId=? and userId = ?";
    @Override
    protected boolean updateData(UserTaoBaoData userTaoBaoData) {
        userTaoBaoData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userTaoBaoData.getItemId()));
        para.put(i++, new DbParameter(Types.INTEGER, userTaoBaoData.getInitPrice()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userTaoBaoData.getLabelList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userTaoBaoData.getSelectedLabelList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userTaoBaoData.getPriceFluctuationList())));
        para.put(i++, new DbParameter(Types.BIGINT, userTaoBaoData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userTaoBaoData.getDealStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, userTaoBaoData.getPlayStatus()));
        para.put(i++, new DbParameter(Types.VARCHAR, userTaoBaoData.getPriceStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, userTaoBaoData.getEarnPrice()));
        para.put(i++, new DbParameter(Types.INTEGER, userTaoBaoData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userTaoBaoData.getUserId()));
        if (getDb().executeNoneQuery(updateUserTaoBaoDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserTaoBaoDataSql = "insert into `t_u_taobao_data`(activityId,userId,itemId,initPrice,labelList,selectedLabelList,priceFluctuationList,resetTime,dealStatus,playStatus,priceStatus,earnPrice) values (?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserTaoBaoData userTaoBaoData) {
        userTaoBaoData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userTaoBaoData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userTaoBaoData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userTaoBaoData.getItemId()));
        para.put(i++, new DbParameter(Types.INTEGER, userTaoBaoData.getInitPrice()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userTaoBaoData.getLabelList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userTaoBaoData.getSelectedLabelList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userTaoBaoData.getPriceFluctuationList())));
        para.put(i++, new DbParameter(Types.BIGINT, userTaoBaoData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, userTaoBaoData.getDealStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, userTaoBaoData.getPlayStatus()));
        para.put(i++, new DbParameter(Types.VARCHAR, userTaoBaoData.getPriceStatus()));
        para.put(i++, new DbParameter(Types.INTEGER, userTaoBaoData.getEarnPrice()));
        if (getDb().executeNoneQuery(insertUserTaoBaoDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserTaoBaoData parseData(ResultSet rs) throws SQLException {
        UserTaoBaoData userTaoBaoData = new UserTaoBaoData();
        userTaoBaoData.setActivityId(rs.getInt("activityId"));
        userTaoBaoData.setUserId(rs.getLong("userId"));
        userTaoBaoData.setItemId(rs.getInt("itemId"));
        userTaoBaoData.setInitPrice(rs.getInt("initPrice"));
        userTaoBaoData.setLabelList(JSON.parseObject(rs.getString("labelList"), new TypeReference<List<TaoBaoLabel>>() {}));
        userTaoBaoData.setSelectedLabelList(JSON.parseObject(rs.getString("selectedLabelList"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        userTaoBaoData.setPriceFluctuationList(JSON.parseObject(rs.getString("priceFluctuationList"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        userTaoBaoData.setResetTime(rs.getLong("resetTime"));
        userTaoBaoData.setDealStatus(rs.getInt("dealStatus"));
        userTaoBaoData.setPlayStatus(rs.getInt("playStatus"));
        userTaoBaoData.setPriceStatus(rs.getString("priceStatus"));
        userTaoBaoData.setEarnPrice(rs.getInt("earnPrice"));

        return userTaoBaoData;
    }
}
